package com.dengqi.mymodule.activity.fragment;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.dengqi.mymodule.BaseActivity;
import com.dengqi.mymodule.R;
import com.dengqi.mymodule.fragment.TypeFragment;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;


public class RadioGroupActivity extends BaseActivity {


    private RadioGroup mRadioGroup;
    //数组 存储Fragment
    private Fragment[] mFragments;
    //当前Fragent的下标
    private Fragment mLastFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_radiogroup);

        mRadioGroup = findViewById(R.id.radioGroup);
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radio = group.findViewById(checkedId);
                Fragment newFragmnet = mFragments[Integer.parseInt(radio.getTag().toString())];
                changeFragment3(newFragmnet);
            }
        });

        int count = mRadioGroup.getChildCount();
        mFragments = new Fragment[count];
        for(int i=0;i<count;i++){
            Bundle bundle = new Bundle();
            bundle.putString("title", "type:"+(i+1));
            mFragments[i] = Fragment.instantiate(this, TypeFragment.class.getName(), bundle);
        }

        ((RadioButton)mRadioGroup.getChildAt(0)).setChecked(true);

    }

    /**
     * A-B A切换到B
     * 切换时不触发生命周期
     */
    public void changeFragment(Fragment newFragment) {
        if(mLastFragment!=newFragment){
            //开启事务
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            //隐藏当前Fragment
            if(mLastFragment!=null){
                ft.hide(mLastFragment);
            }
            //判断Fragment是否已经添加
            if (!newFragment.isAdded()) {
                ft.add(android.R.id.tabcontent, newFragment);
            }
            //显示新的Fragment
            ft.show(newFragment);
            ft.commit();
            mLastFragment = newFragment;
        }
    }

    /**
     * A-B A切换到B
     * onCreateB-onPauseA-onStopA-onDestroyViewA-onDestroyA-onStartB-onResumeB
     */
    public void changeFragment2(Fragment newFragment) {
        //开启事务
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(android.R.id.tabcontent, newFragment);
        ft.commit();
    }

    /**
     * tabhost源码
     * A-B A切换到B
     * onPauseA-onStopA-onDestroyViewA-onStartB-onResumeB
     */
    public void changeFragment3(Fragment newFragment) {
        if(mLastFragment!=newFragment){
            //开启事务
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            //隐藏当前Fragment
            if(mLastFragment!=null){
                ft.detach(mLastFragment);
            }
            //判断Fragment是否已经添加
            if (!newFragment.isAdded()) {
                ft.add(android.R.id.tabcontent, newFragment);
            }
            //显示新的Fragment
            ft.attach(newFragment);
            ft.commit();
            mLastFragment = newFragment;
        }
    }

}
